<?php

namespace App\JsonRpc;

use App\Model\Moment;
use Hyperf\RpcServer\Annotation\RpcService;

/**
 * 注意，如希望通过服务中心来管理服务，需在注解内增加 publishTo 属性
 */
#[RpcService(name: 'MomentService', server: 'jsonrpc-http', protocol: 'jsonrpc-http',publishTo: 'nacos')]
class MomentService implements MomentServiceInterface
{
    public function list(int $page, int $pageSize, int $type): array
    {
        $total = Moment::query()->where('show_type', '=', $type)->where('is_show', '=', 0)
            ->where('status', '=', 1)->count();
        $list = Moment::query()->where('show_type', '=', $type)->where('is_show', '=', 0)
            ->where('status', '=', 1)
            ->forPage($page, $pageSize)
            ->get();
        return [
            'code' => 0,
            // 服务名称
            'service' => 'MomentService',
            'action' => 'list',
            'data' => [
                'list' => $list,
                'type' => $type,
                'page' => $page,
                'pageSize' => $pageSize,
                'total' => $total,
            ],
        ];
    }

    public function create(array $data): array
    {
        $data = Moment::create($data);
        return [
            'code' => 0,
            'service' => 'MomentService',
            'action' => 'create',
            'data' => $data,
        ];
    }

    public function delete(int $id): array
    {
        return [
            'code' => 0,
            'service' => 'MomentService',
            'action' => 'delete',
            'data' => [
                'id' => Moment::destroy($id),
            ],
        ];
    }

    public function update(int $id, array $update): array
    {
        $updateResult = Moment::query()->where('id', '=', $id)->update($update);
        return [
            'code' => 0,
            'service' => 'MomentService',
            'action' => 'update',
            'data' => [
                'id' => $id,
                'update' => $updateResult,
            ],
        ];
    }

    public function detail(int $id): array
    {
        return [
            'code' => 0,
            'service' => 'MomentService',
            'action' => 'detail',
            'data' => [
                'id' => Moment::find($id),
            ],
        ];
    }
}
